package com.mingchuang.administrator.mingchuang.pile;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.exception.DbException;
import com.mingchuang.administrator.mingchuang.AppApplication;
import com.mingchuang.administrator.mingchuang.GetLocationActivity;
import com.mingchuang.administrator.mingchuang.R;
import com.mingchuang.administrator.mingchuang.bean.PileInfoBean;
import com.mingchuang.administrator.mingchuang.project.ProjectActivity;

import java.util.List;


/**
 * Created by Administrator on 2016/1/18.
 *
 * 工程详情页面 显示全部信息
 */
public class PileListActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, View.OnClickListener {

    private ListView listView;
    private List<PileInfoBean> uploadList;

    private Button upload_btn;

    private  String projectName;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.pile_list_layout);
        projectName = getIntent().getStringExtra("name");


        iintView();
    }

    private void iintView() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle("基桩列表");



        listView = (ListView) findViewById(R.id.list_content);


        DbUtils db = AppApplication.getDb();


        try {
            //uploadList = db.findAll(PileInfoBean.class);
            Selector select =Selector.from(PileInfoBean.class).where("projectName","=",projectName);
            uploadList = db.findAll(select);

            Log.d("kipeng","uploadList "+uploadList.size());

            for (PileInfoBean pile : uploadList) {
                Log.d("kipeng", pile.toString());
            }

            PileAdapter adapter = new PileAdapter(this, uploadList);
            listView.setAdapter(adapter);


        } catch (DbException e) {
            e.printStackTrace();
        }

        listView.setOnItemClickListener(this);

        upload_btn = (Button) findViewById(R.id.upload_btn);
        upload_btn.setOnClickListener(this);



    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                break;

            default:
                break;
        }
        return super.onOptionsItemSelected(item);




    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //点击进入基桩详情页面

        Intent intent = new Intent();
        intent.setClass(this , PileInfoActivity.class);
        intent.putExtra("pileInfo",uploadList.get(position));

        startActivity(intent);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.upload_btn:

                break;

            case R.id.get_gps_btn:
                //定位
                Intent intent = new Intent();
                intent.setClass(this, GetLocationActivity.class);
                intent.putExtra("path",pileInfo.getPath());
                intent.putExtra("id",pileInfo.getId());
                intent.putExtra("pileInfo",pileInfo);
                startActivity(intent);
                break;

            case R.id.take_photo_btn:
                //拍照
                Intent intent2 = new Intent();
                intent2.setClass(this, com.mingchuang.administrator.mingchuang.photo.activity.MainActivity.class);
                intent2.putExtra("path",pileInfo.getPath());
                intent2.putExtra("id",pileInfo.getId());
                startActivity(intent2);

                break;
            default:

                break;



        }
    }
}
